package com.huaxin.device.mapping;

import com.huaxin.device.dto.request.ElectricalRoomCreateReqDTO;
import com.huaxin.device.dto.request.ElectricalRoomUpdateReqDTO;
import com.huaxin.device.models.ElectricalRoom;
import com.huaxin.hxenum.enums.FireRating;
import com.huaxin.hxenum.enums.VentilationType;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;

@Mapper(componentModel = "spring")
public interface ElectricalRoomMapping {

    @Mapping(target = "fireRating", ignore = true)
    @Mapping(target = "ventilationType", ignore = true)
    ElectricalRoom toEntity(ElectricalRoomCreateReqDTO in);

    @AfterMapping
    default void afterMapping(ElectricalRoomCreateReqDTO in, @MappingTarget ElectricalRoom entity) {
        if (in.getFireRating() != null) {
            entity.setFireRating(FireRating.of(in.getFireRating()));
        }
        if (in.getVentilationType() != null) {
            entity.setVentilationType(VentilationType.of(in.getVentilationType()));
        }
    }

    @Mapping(target = "fireRating", ignore = true)
    @Mapping(target = "ventilationType", ignore = true)
    void updateEntity(ElectricalRoomUpdateReqDTO in, @MappingTarget ElectricalRoom entity);

    @AfterMapping
    default void afterMapping(ElectricalRoomUpdateReqDTO in, @MappingTarget ElectricalRoom entity) {
        if (in.getFireRating() != null) {
            entity.setFireRating(FireRating.of(in.getFireRating()));
        }
        if (in.getVentilationType() != null) {
            entity.setVentilationType(VentilationType.of(in.getVentilationType()));
        }
    }
}